package main

import (
	// "fmt"
	"log"
	"net/rpc"
	"time"

	"rtsp1/source/server"
)

func main() {
	client, err := rpc.DialHTTP("tcp", "127.0.0.1:8081")
	if err != nil {
		log.Fatal(err.Error())
	}

	//GetList
	var list []string
	err = client.Call("Server.GetList", 0, &list)
	if err != nil {
		log.Fatal(err.Error())
	}
	log.Println(list)
	time.Sleep(time.Second)

	//Open
	//Open the right source
	var reply server.Reply
	var pid int

	log.Println("Open right")
	err = client.Call("Server.Open", list[0], &reply)
	if err != nil {
		log.Fatal(err.Error())
	}
	log.Printf("address:%s \npid:%d \n", reply.Address, reply.Pid)
	pid = reply.Pid
	time.Sleep(time.Second)

	//Open the something not have
	log.Println("Open wrong")
	err = client.Call("Server.Open", "something", &reply)
	if err != nil {
		log.Println(err.Error())
	} else {
		log.Printf("address:%s \npid:%d \n", reply.Address, reply.Pid)
	}
	time.Sleep(time.Second)

	//Open the right source twice
	log.Println("Open right twice")
	err = client.Call("Server.Open", list[0], &reply)
	if err != nil {
		log.Fatal(err.Error())
	}
	log.Printf("address:%s \npid:%d \n", reply.Address, reply.Pid)
	time.Sleep(time.Second)

	//Close
	var feedBack string

	//Close right
	log.Println("Close with the right pid")
	err = client.Call("Server.Close", pid, &feedBack)
	if err != nil {
		log.Println(err.Error())
	} else {
		log.Println("feedBack:", feedBack)
	}
	time.Sleep(time.Second)

	//Close right again
	log.Println("Close with the right pid")
	err = client.Call("Server.Close", pid, &feedBack)
	if err != nil {
		log.Println(err.Error())
	} else {
		log.Println("feedBack:", feedBack)
	}
	time.Sleep(time.Second)

	//Close right last time
	log.Println("Close with the right pid again")
	err = client.Call("Server.Close", pid, &feedBack)
	if err != nil {
		log.Println(err.Error())
	} else {
		log.Println("feedBack:", feedBack)
	}
	time.Sleep(time.Second)
}
